package com.tgu.home_action.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;

@Configuration
public class HttpService {
    @Autowired
    private Logger logger;
    @Autowired
    private AliYunService aliYunService;
    @Autowired
    private PoolingHttpClientConnectionManager poolConnManager;
    @Autowired
    private RequestConfig requestConfig;

    public CloseableHttpClient getConnection() {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();
        return HttpClients.custom()
                // 设置连接池管理
                .setConnectionManager(poolConnManager)
                .setDefaultRequestConfig(config)
                // 设置重试次数
                .setRetryHandler(new DefaultHttpRequestRetryHandler(2, false)).build();
    }

    /**
     * 发起Get请求（无参）
     *
     * @param url：请求地址
     * @return map
     */
    public String httpGet(String url) {
        CloseableHttpClient httpClient = getConnection();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpStatus.SC_OK) {
                return result;
            } else {
                logger.error("请求{}返回错误码：{},{}", url, code, result);
                return null;
            }
        } catch (IOException e) {
            logger.error("http请求异常，{}", url, e);
        } finally {
            try {
                if (response != null)
                    response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 有参GET请求
     *
     * @param url：请求地址
     * @param param：参数
     * @return map
     */
    public String httpGet(String url, Map<String, Object> param) {
        CloseableHttpClient httpClient = getConnection();
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        int i = 0;
        StringBuilder urlBuilder = new StringBuilder(url);
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            if (i == 0) {
                urlBuilder.append("?").append(entry.getKey()).append("=").append(entry.getValue());
            } else {
                urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
            i++;
        }
        url = urlBuilder.toString();
        System.out.println(url);
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpStatus.SC_OK) {
                return result;
            } else {
                logger.error("请求{}返回错误码：{},{}", url, code, result);
                return null;
            }
        } catch (IOException e) {
            logger.error("http请求异常，{}", url, e);
        } finally {
            try {
                if (response != null)
                    response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String post(String uri, Object params) {
        CloseableHttpClient httpClient = getConnection();
        HttpPost httpPost = new HttpPost(uri);
        CloseableHttpResponse response = null;
        try {
            StringEntity paramEntity = new StringEntity(JSON.toJSONString(params), StandardCharsets.UTF_8);
            paramEntity.setContentEncoding("UTF-8");
            paramEntity.setContentType("application/json;charset=utf-8");
            httpPost.setEntity(paramEntity);
            response = httpClient.execute(httpPost);
            int code = response.getStatusLine().getStatusCode();
            String result = EntityUtils.toString(response.getEntity());
            if (code == HttpStatus.SC_OK) {
                return result;
            } else {
                logger.error("请求{}返回错误码:{},请求参数:{},{}", uri, code, params, result);
                return null;
            }
        } catch (IOException e) {
            logger.error("收集服务配置http请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public Map<String, Object> postReturnMap(String uri, Object params) {
        CloseableHttpClient httpClient = getConnection();
        HttpPost httpPost = new HttpPost(uri);
        CloseableHttpResponse response = null;
        try {
            StringEntity paramEntity = new StringEntity(JSON.toJSONString(params), StandardCharsets.UTF_8);
            paramEntity.setContentEncoding("UTF-8");
            paramEntity.setContentType("application/json;charset=utf-8");
            httpPost.setEntity(paramEntity);
            response = httpClient.execute(httpPost);
            int code = response.getStatusLine().getStatusCode();
            String result = EntityUtils.toString(response.getEntity());
            if (code == HttpStatus.SC_OK) {
                return (Map<String, Object>) JSONObject.parse(result);
            } else {
                logger.error("请求{}返回错误码:{},请求参数:{},{}", uri, code, params, result);
                return null;
            }
        } catch (IOException e) {
            logger.error("收集服务配置http请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public InputStream postGetInputStream(String uri, Map<String, Object> params) {
        CloseableHttpClient httpClient = getConnection();
        HttpPost httpPost = new HttpPost(uri);
        CloseableHttpResponse response = null;
        try {
            StringEntity paramEntity = new StringEntity(JSON.toJSONString(params), StandardCharsets.UTF_8);
            paramEntity.setContentEncoding("UTF-8");
            paramEntity.setContentType("application/json;charset=utf-8");
            httpPost.setEntity(paramEntity);
            response = httpClient.execute(httpPost);
            int code = response.getStatusLine().getStatusCode();
            return response.getEntity().getContent();
//

//            if (code == HttpStatus.SC_OK) {
//                return result;
//            } else {
//                logger.error("请求{}返回错误码:{},请求参数:{},{}", uri, code, params,result);
//                return null;
//            }
        } catch (IOException e) {
            logger.error("收集服务配置http请求异常", e);
        } finally {
//            try {
//                if(response != null) {
//                    response.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
        return null;
    }

//    public String sendPostByForm(String url, Map<String,String> map,int reSend) {
//        //声明返回结果
//        String result = "";
//        //开始请求API接口时间
//        long startTime=System.currentTimeMillis();
//        //请求API接口的响应时间
//        long endTime= 0L;
//        HttpEntity httpEntity = null;
//        UrlEncodedFormEntity entity = null;
//        HttpResponse httpResponse = null;
//        CloseableHttpClient httpClient = null;
//        try {
//            // 创建连接
//             httpClient=getConnection();
//            // 设置请求头和报文
//            HttpPost httpPost = new HttpPost(url);
//            //设置参数
//            List<NameValuePair> list = new ArrayList<NameValuePair>();
//            Iterator iterator = map.entrySet().iterator();
//            while(iterator.hasNext()){
//                Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
//                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
//            }
//            entity = new UrlEncodedFormEntity(list,HttpConstant.UTF8_ENCODE);
//            httpPost.setEntity(entity);
//            logger.info("请求{}接口的参数为{}",url,map);
//            //执行发送，获取相应结果
//            httpResponse = httpClient.execute(httpPost);
//            httpEntity= httpResponse.getEntity();
//            result = EntityUtils.toString(httpEntity);
//        } catch (Exception e) {
//            logger.error("请求{}接口出现异常",url,e);
//            if (reSend > 0) {
//                logger.info("请求{}出现异常:{}，进行重发。进行第{}次重发",url,e.getMessage(),(HttpConstant.REQ_TIMES-reSend +1));
//                result = sendPostByForm(url, map, reSend - 1);
//                if (result != null && !"".equals(result)) {
//                    return result;
//                }
//            }
//        }finally {
//            try {
//                EntityUtils.consume(httpEntity);
//            } catch (IOException e) {
//                logger.error("http请求释放资源异常",e);
//            }
//        }
//        //请求接口的响应时间
//        endTime=System.currentTimeMillis();
//        logger.info("请求{}接口的响应报文内容为{},本次请求API接口的响应时间为:{}毫秒",url,result,(endTime-startTime));
//        return result;
//
//    }
}
